package cn.sumpu.app.guoguojie.web.mvc.family;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.sumpu.app.guoguojie.biz.ContactBiz;
import cn.sumpu.app.guoguojie.biz.DetailBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.domain.Contact;
import cn.sumpu.app.guoguojie.domain.Detail;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Encode;

@Controller
public class ContactController {

	@Autowired
	private ContactBiz contactBiz;
	@Autowired
	private DetailBiz detailBiz;
	@Autowired
	private MemberBiz memberBiz;

	// 联系信息
	@RequestMapping(value = "/enterContact.htm")
	public String enterContactInfo(String memberId, HttpServletRequest request) {
		try {
			Member member = (Member) request.getSession().getAttribute("member");
			if(memberId != null && !"".equals(memberId) && !member.getIdentifier().equals(memberId)){
				member = memberBiz.getMemberById(memberId);
			}
			List<Contact> contactList = contactBiz.getContactListMember(member.getIdentifier());
			Detail detail = detailBiz.getDetailById(member.getIdentifier());
			if (contactList != null) {
				detail.setContactList(contactList);
				request.setAttribute("contactList", contactList);
			}
			request.setAttribute("detail", detail);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "member/memContact";
	}

	// 更新联系信息
	@RequestMapping(value = "/saveContact.htm", method = RequestMethod.POST)
	public String saveContact(String memberId, HttpServletRequest request,
			HttpServletResponse response) {

		Member member = (Member) request.getSession().getAttribute("member");
		Detail detail = null;
		response.setContentType("text/plain; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			String escaped_json = request.getParameter("jsonData");// 获取提交的参数值,jsonData与上面红体字的jsonData相对应
			String json = Encode.unescape(escaped_json);// 将得到的json,进行unescape解析,(Encode是我自定义的类)
			JSONObject jsonObject = JSONObject.fromObject(json);
			Map<String, Class<Contact>> m = new HashMap<String, Class<Contact>>();
			m.put("contactList", Contact.class);// "contactList"与js
												// object里的contactList对应,contactList.class类的字段与客户端new
												// Product(........);具有相同的数据结构
			detail = (Detail) JSONObject.toBean(jsonObject, Detail.class, m);
			if(memberId != null && !"".equals(memberId) && !member.getIdentifier().equals(memberId)){
				detail.setMember(memberId);
			}else{
				detail.setMember(member.getIdentifier());
			}
			contactBiz.addDetail(detail);
			pw.print("success");

		} catch (Exception e) {
			e.printStackTrace();
			pw.print("error");
		}
		return null;
	}

}
